#!/usr/bin/env python

# Volturnus Address Book v2 svn

# -----------------------------------------------------------------------
# Copyright (C) 2008 Bryan Smith

# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.

# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.

# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
# -----------------------------------------------------------------------

from SimpleXMLRPCServer import SimpleXMLRPCServer
import socket
import sqlite3
import time
import config

# Vol Server Class
class Volserver:
	
	server_host = socket.gethostname()
	con = ""
	cur = ""
	log = config.log_file

	def remote_view_lastname(self, last_name):
		names = ""
		try:
			get_info = "SELECT * from voldb WHERE lname like '%" + last_name + "%'"
			self.cur.execute(get_info)
		except StandardError:
			print "Retrieval failed. Check read permissions."
		for info in self.cur.fetchall():
			names += "\n" + info[0] + ", " + info[1] + "\n" + \
        	          info[2] + "\n" + info[3] + ", " + \
        	          info[4] + ", " + info[6] + "\n" + \
        	          info[5] + "\n" + "Home: " + info[7] + \
        	          "\nWork: " + info[8] + "\n"
        	return names

	def remote_view_names(self):
		names = ""
		try:
			get_info = "SELECT * from voldb"
			self.cur.execute(get_info)
		except StandardError:
			print "Retrieval failed. Check read permissions."
		for info in self.cur.fetchall():
			names += "\n" + info[0] + ", " + info[1] + "\n" + \
        	          info[2] + "\n" + info[3] + ", " + \
        	          info[4] + ", " + info[6] + "\n" + \
        	          info[5] + "\n" + "Home: " + info[7] + \
        	          "\nWork: " + info[8] + "\n"
        	return names

	def establish_connection(self, hostname):
		message = ">> Connection established by " + hostname + " at " + time.asctime()
		print message
		if self.log == True:
			log = open("vol_server.log", "a")
			log.write(message)
			log.close
		return "Successful connection to " + self.server_host 
			
	def __init__(self):
		try:
			self.con = sqlite3.connect("volturnus.db") # Connect to the database
			self.cur = self.con.cursor() # Create a cursor
		except Exception:
			print "Error establishing connection to database."

		try:
			serv = SimpleXMLRPCServer(("", 9876))
			serv.register_function(self.establish_connection)
			serv.register_function(self.remote_view_names)
			serv.register_function(self.remote_view_lastname)
			print "==============================================\n" + \
                  "| Volturnus Address Book Server (Vol Server) |\n" + \
                  "==============================================\n\n" + \
                  "Written by Bryan Smith. (c) 2008 Bryan Smith.\n"
			print ">> Vol Server Started (" + time.asctime() + ")"
			serv.serve_forever()
		except:
			print ">> Vol Server Stopped"

if __name__ == "__main__":
	vs = Volserver()
